const joi = require('joi');
const { dealNotEmpty } = require('../../utils/dberrordeal');
const AddressController = require('../../controllers/good/addressController');
const validationMiddleware = require('../../middleware/validate');
const router = require('koa-router')();
router.prefix('/address');
const { verify } = require('../../utils/auth');
router.use(verify);
const rulesSchema = {
    '/address/addaddress': joi.object({
        name: dealNotEmpty('收货人姓名'),
        phone: joi.string().length(11).pattern(/^1[3-9]\d{9}$/).required().messages({
            'string.pattern.base': '手机号格式不正确',
            'string.length': '手机号长度必须为11位',
            'any.required': '手机号是必填字段',
        }),
        address: dealNotEmpty('详细地址'),
        //省市区
        province: dealNotEmpty('省'),
        city: dealNotEmpty('市'),
        county: dealNotEmpty('区'),
    }),
    '/address/putaddress': joi.object({
        id: dealNotEmpty('地址id'),
        name: dealNotEmpty('收货人姓名'),
        phone: joi.string().length(11).pattern(/^1[3-9]\d{9}$/).required().messages({
            'string.pattern.base': '手机号格式不正确',
            'string.length': '手机号长度必须为11位',
            'any.required': '手机号是必填字段',
        }),
        address: dealNotEmpty('详细地址'),
        //省市区
        province: dealNotEmpty('省'),
        city: dealNotEmpty('市'),
        county: dealNotEmpty('区'),
    }),
    '/address/getaddress': joi.object({
        id: dealNotEmpty('地址id'),
    }),
    '/address/deladdress': joi.object({
        id: dealNotEmpty('地址id'),
    }),
};
router.post('/addaddress', validationMiddleware(rulesSchema), AddressController.createAddress);
router.post('/putaddress', validationMiddleware(rulesSchema), AddressController.updateAddress);
router.post('/getaddress', validationMiddleware(rulesSchema), AddressController.getAddressById);
router.post('/deladdress', validationMiddleware(rulesSchema), AddressController.deleteAddress);
router.post('/getaddresslist', AddressController.getAddressList);
module.exports = router;
